/**
 * @author yinyuejia
 * @file 封装数组常用方法
 * @include 设置 set 获取 get 删除 del
 */

const cookie = {
  /**
   * 写cookies
   * time:单位：秒
   */
  set(name: any, value: any, time: any) {
    time = time || 7 * 24 * 60 * 60;
    const exp = new Date();
    exp.setTime(exp.getTime() + time * 1000);
    document.cookie = `${name}=${escape(value)};expires=${exp.toUTCString()}`;
  },
  get(name: any) {
    const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
    const arr = document.cookie.match(reg);
    if (arr) return unescape(arr[2]);
    return undefined;
  },
  del(name: any) {
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    const cval = this.get(name);
    if (cval) {
      document.cookie = `${name}=${cval};expires=${exp.toUTCString()}`;
    }
  },
};
export default cookie;
